<template>
  <div class="page">

    <MCard :type="type" :cards="cards" @getLickGoods="getLickGoods"></MCard>

    <div class="none" v-if="cards.length==0">
      <img src="../../../static/images/lottery/none.png" alt="">
    </div>
  </div>
</template>

<script>
import MCard from '../../components/MCard.vue'
export default {
  data () {
    return {
      isNone: false,
      type: '中奖',
      cards: [],
      openid: ''
    }
  },
  components: {
    MCard
  },

  mounted () {
    this.openid = wx.getStorageSync('openid')
    this.getexchangeHistory()
  },

  methods: {
    getexchangeHistory () {
      this.$http.get('/api/v1/luckhis?openid=' + this.openid).then(res => {
        if (res.data.code === '0') {
          for (let item of res.data.data) {
            item.date = item.luck_time
            item.name = item.luck_good_name
            item.img = item.image_src
          }
          this.cards = res.data.data
        } else {
          console.log('商品列表失败')
        }
      })
    },

    getLickGoods (item) {
      console.log(item)
      this.$http.post('/api/v1/gift/receive', {
        body: {
          order_serial: item.order_serial
        }
      }).then(res => {
        console.log(res)
        if (res.data.code === '0') {
          wx.showModal({
            content: '领取成功，请耐心等待奖品的发放',
            showCancel: false
          })
          this.getexchangeHistory()
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.none {
  text-align: center;
  margin-top: 240rpx;
}
.none img {
  height: 170rpx;
  width: 533rpx;
}
</style>
<style>
page {
  background: #f1f1f1;
}
</style>